//
//  DHS2_SearchAll_CommunityCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/7.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchAll_CommunityCell.h"
#import "DHCommunity_NoImageCell.h"
#import "DHCommunityDetailController.h"

@interface DHS2_SearchAll_CommunityCell()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSArray *itemArr;

@end

@implementation DHS2_SearchAll_CommunityCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCommunity_NoImageCell" bundle:nil] forCellReuseIdentifier:kDHCommunity_NoImageCell];
}

- (void)uploadWithArr:(NSArray *) arr{
    self.itemArr = arr;
    [self.tableView reloadData];
}
- (IBAction)allAction:(id)sender {
    [self routerEventWithName:@"cou_all" userInfo:nil];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    NSArray *images = [model objectForKey:@"images"];
    
    //计算文字高度
    CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
    //如果内容中有 \n 等转义字符
    NSInteger specialChar = 0;//出现次数
    specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
    CGFloat textHeight = contentHeight + specialChar * 14;
    
    //图片CollectionView高度
    CGFloat imgsHeight = 0;
    if (images.count == 0) {
        imgsHeight = 0;
    }else if (images.count > 0 && images.count <= 3){
        imgsHeight = 128;
    }else if (images.count > 3 && images.count <= 6){
        imgsHeight = 128 * 2 + 5;
    }else if (images.count > 6 && images.count <= 9){
        imgsHeight = 128 * 3 + 10;
    }
    
    return imgsHeight + textHeight + 133;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCommunity_NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCommunity_NoImageCell forIndexPath:indexPath];
    [cell updateCell:[self.itemArr objectAtIndex:indexPath.row]];
    cell.row = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHCommunityDetailController *vc = [[DHCommunityDetailController alloc] initWithCommunityDic:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
